package com.webframe.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.webframe.constant.WFConstant;
import com.webframe.service.def.IUserManageService;
import com.webframe.util.UUIDGenerator;

@Controller
@RequestMapping("/realEstate/userManage")
public class UserManageController extends BaseController
{
	@Autowired
	private IUserManageService userManageService;
	/**
 	 * 注册
 	 * @param request
 	 * @param response
 	 * @return
 	 * @throws Exception
 	 */
 	@RequestMapping("/regist")
	public String regist(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String login_name_ = request.getParameter("login_name_");
		String real_name_ = request.getParameter("real_name_");
		String email_ = request.getParameter("email");
		String password_ = request.getParameter("password_");
		String org_id_ = request.getParameter("org_id_");
		String status = "2";//表示审批中
		String msg = "";
		if (org_id_==null || "".equals(org_id_.trim()) || password_==null || "".equals(password_.trim()) || login_name_==null || "".equals(login_name_.trim()) || real_name_==null || "".equals(real_name_.trim())) {
			msg = "系统检测到错误参数,添加用户失败,请刷新页面重试.";
			request.setAttribute("msg", msg);
			return "/realEstate/userRegister";
		}
		try {
			if(submissionPermission(request))
			{
				userManageService.addUser(login_name_, real_name_, email_, password_, org_id_, status);
			}
		} catch (org.springframework.dao.DuplicateKeyException e) {
			msg = "用户名已存在!";
		} catch (Exception e) {
			if (e instanceof org.springframework.dao.DataIntegrityViolationException) {
				msg = "用户名已存在!";
			}else{
				msg = "注册失败，请重试！";
			}
			e.printStackTrace();
		}
		request.setAttribute("msg", msg);
		return msg == ""?"realEstate/registSucess":"realEstate/userRegister";
	}
 	@RequestMapping("/checkOrgNameExist")
 	public String checkOrgNameExist(HttpServletRequest request,HttpServletResponse response) throws Exception
 	{
 		String orgName = request.getParameter("orgName");
 		boolean exist = userManageService.checkExist(orgName);
 		String msg = exist ? "{exist:true}" : "{exist:false}";
 		outWrite(response, msg);
 		return null;
 	}
 	
 	private boolean submissionPermission(HttpServletRequest request)
	{
		// 如果是Ajax请求，不处理重复提交
		if ("true".equalsIgnoreCase(request.getParameter(WFConstant.IS_AJAX_))) 
		{
			return true;
		}
		String key1 = request.getParameter(WFConstant.Constant_Duplicate_Submission_KEY_);
		String key2 = "";
		if (request.getSession().getAttribute(WFConstant.Constant_Duplicate_Submission_KEY_) != null)
		{
			key2 = request.getSession().getAttribute(WFConstant.Constant_Duplicate_Submission_KEY_).toString();
		}
		request.getSession().setAttribute(WFConstant.Constant_Duplicate_Submission_KEY_,UUIDGenerator.getUUID());// 处理重复提交
		return (key1.equals(key2));
	}
}
